﻿using Microsoft.Extensions.PlatformAbstractions;
using System;
using System.IO;
using NLog;
using PeterKottas.DotNetCore.WindowsService;

namespace WebSvrUpdaterCore
{
    class Program
    {
        private static Logger _logger = LogManager.GetCurrentClassLogger();
        static void Main(string[] args)
        {
            
            ServiceRunner<WebSvrUpdaterService>.Run(config =>
            {
                //var name = config.GetDefaultName();
                var name = "WebSvrUpdaterCoreService";
                config.SetName(name);
                config.SetDisplayName(name);
                config.SetDescription("满天星Web更新服务");
                config.Service(serviceConfig =>
                {
                    serviceConfig.ServiceFactory((extraArguments, controller) =>
                    {
                        return new WebSvrUpdaterService(controller);
                    });

                    serviceConfig.OnStart((svc, extraParams) =>
                    {
                        //Console.WriteLine("服务 {0} 已启动", name);
                        svc.Start();
                    });

                    serviceConfig.OnStop(svc =>
                    {
                        //Console.WriteLine("服务 {0} 已停止", name);
                        svc.Stop();
                    });

                    serviceConfig.OnInstall(svc =>
                    {
                        //Console.WriteLine("服务{0} 已安装", name);
                        _logger.Info($"服务{name},已安装");                        
                    });

                    serviceConfig.OnUnInstall(svc =>
                    {
                        //Console.WriteLine("服务{0} 已卸载", name);
                        _logger.Info($"服务{name},已卸载");
                    });

                    serviceConfig.OnError(e =>
                    {
                        _logger.Error(e.GetBaseException(), e.Message);
                        //Console.WriteLine("服务 {0} 发生错误,异常信息 : {1}", name, e.Message);
                    });
                });
            });
        }
    }
}
